﻿using System;
using System.Collections.Generic;
using PharmacyPharnet.Core;
using Pharmacy.Ultilities.Resources;
using Pharmacy.Business.Login;
using Pharmacy.Ultilities;
using Pharmacy.Ultilities.Configurations;
using Pharmacy.Data.Entity;
using System.Text;
using System.Windows.Forms;


namespace PharmacyPharnet
{
    public partial class FrmLogin : DialogViewBase
    {              
        #region Declarations
        public const int ILOGINHIDE = 0;
        public const int ILOGINSHOW = 1;
        //public delegate void AuthenticatedHandler(User user);
        public event AuthenticatedHandler Authenticated;
        User currentUser = null;
        #endregion

        #region Constructors
        public FrmLogin()
        {
            InitializeComponent();            
        }
        #endregion

        #region Override methods
        
        #endregion

        #region Custom methods
        private bool ValidateInformation()
        {
            bool result = true;
            if (txtUserID.Text.Length == 0)
            {
                txtUserID.ErrorText = string.Format(SR.Instance.GetString(Constant.KEY_EMPTYINFORMATION), lblUserName.Text.Trim());
                result = result & false;
            }
            if (txtPassword.Text.Length == 0)
            {
                txtPassword.ErrorText = string.Format(SR.Instance.GetString(Constant.KEY_EMPTYINFORMATION), lblPassword.Text.Trim());
                result = result & false;
            }
            if(result == false)
                return result;
            LoginBO loginBO = new LoginBO();
            StringBuilder errorMsg ;
            currentUser = loginBO.ValidateUserName(txtUserID.Text.Trim(), txtPassword.Text.Trim(), out errorMsg);
            if (errorMsg.Length > 0)
            {
                lblMessage.Text = errorMsg.ToString();
                result = false;
            }            
            return result;
        }

        /// <summary>
        /// Hide the login form
        /// </summary>
        public void HideThis()
        {
            if (InvokeRequired)
            {
                Invoke(new MethodInvoker(HideThis));
            }
            else
            {
                Opacity = ILOGINHIDE;
                ShowInTaskbar = false;
                Hide();
            }
        }

        /// <summary>
        /// Show the login form
        /// </summary>
        public void ShowThis()
        {
            if (this.InvokeRequired)
            {
                this.Invoke(new MethodInvoker(ShowThis));
            }
            else
            {
                this.Opacity = ILOGINSHOW;
                this.ShowInTaskbar = true;
                this.Show();
            }
        }

        /// <summary>
        /// Authenticate user
        /// </summary>
        /// <param name="user">user</param>
        private void OnAuthenticated(User user)
        {
            Global.UserName = currentUser.UserName;
            Global.UserID = currentUser.ID;

            UserRole objUserRole = LinqToObject.From<UserRole>(currentUser.UserRoles)
                                                .Where(delegate(UserRole ur) { return ur.Role.Name == "Administrator"; })
                                                .First();
            if(objUserRole != null)
                Global.CurrentUserIsAdmin = true;
            

            if (Authenticated != null)
            {
                if (this.InvokeRequired)
                {
                    this.Invoke(new AuthenticatedHandler(OnAuthenticated), user);
                }
                else
                {
                    Authenticated(currentUser);
                }
            }
        }
        #endregion

        #region Event Handlers
        private void btnLogin_Click(object sender, EventArgs e)
        {
            if (ValidateInformation() == false)
            {
                return;
            }
            else
            {
                HideThis();
                OnAuthenticated(currentUser);
            }
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void txtPassword_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)Keys.Enter)
            {
                btnLogin.PerformClick();
            }
        }

        private void txtUserID_Enter(object sender, EventArgs e)
        {
            txtUserID.SelectAll();
        }

        private void txtPassword_Enter(object sender, EventArgs e)
        {
            txtPassword.SelectAll();
        }

        private void FrmLogin_Load(object sender, EventArgs e)
        {
#if DEBUG
            txtUserID.Text = "tam";
            txtPassword.Text = "sa123456";
#endif
        }

        private void btnConfig_Click(object sender, EventArgs e)
        {
            FrmConfiguration frmConfig = new FrmConfiguration();
            frmConfig.ShowDialog();
        }
        #endregion                                        

        
    }
}